#!/bin/bash
#
# Copyright (C) 2013 CIeNET, all rights reserved
#
# This is internal script. Don't use it unless you know what you are doing!!!
source ~/.bash_profile

#env setup
cd `echo ${0%/*}`
export  AUTODEPLOY_HOME=`pwd`  #&& echo AUTODEPLOY_HOME=$AUTODEPLOY_HOME
. $AUTODEPLOY_HOME/setEnv.sh
[ $? -eq 0 ] || fail "env setup faild,check $AUTODEPLOY_HOME/setEnv.sh please."

#log_s arg0=$0 arg1=$1 arg2=$2 arg3=$3
#get defalt app name
if ["$3" eq ""]
then
APP_NAME="Sandglass";
else 
APP_NAME=$3
fi
log_s "Target app name:$APP_NAME"
#update source code by git
function updateCode()
{ 
	#log_s "GIT updateCode is starting..." 
	cd $GIT_REPO
	git reset --hard origin/master >> $AUTODEPLOY_LOG_FILE
	git pull $GIT_PATH master >> $AUTODEPLOY_LOG_FILE
	log_s `git status`
    [ $? -eq 0 ] || fail "GIT updateCode faild: $?"

	cp ./Sandglass/src/main/resources/com/cienet/sandglass/config/framework/jdbc.properties.$ENV_FLAG ./Sandglass/src/main/resources/com/cienet/sandglass/config/framework/jdbc.properties
	[ $? -eq 0 ] || fail "GIT updateCode: Copy jdbc.properties.$ENV_FLAG faild: "
	cp ./Sandglass/src/main/resources/com/cienet/sandglass/config/framework/deploy.properties.$ENV_FLAG ./Sandglass/src/main/resources/com/cienet/sandglass/config/framework/deploy.properties
	[ $? -eq 0 ] || fail "GIT updateCode: Copy deploy.properties.$ENV_FLAG faild: "
	
	log_s "GIT updateCode is completed" 
}

#compile code by maven
function compile()
{
	updateCode
	#log_s "maven compile is starting..."
	cd $GIT_REPO/$APP_NAME
	$M2_HOME/bin/mvn clean compile >> $AUTODEPLOY_LOG_FILE
    [ $? -eq 0 ] || fail "maven compile faild: $?"
	log_s "maven compile completed."
}

#package war by maven
function package()
{
	#git remote add origin git@10.80.1.44:ccwp.git;
	#git pull origin master
	updateCode
	
	#log_s "maven package is starting..." 
	cd $GIT_REPO/$APP_NAME
	$M2_HOME/bin/mvn clean package >> $AUTODEPLOY_LOG_FILE
	[ $? -eq 0 ] || fail "maven package faild: $?  "
    #log_s `tail -7 $AUTODEPLOY_LOG_FILE|grep -v "\-\-\-\-\-"`
	log_s "maven package completed" 
}

#cp war file to tomcat & restart
function deploy()
{
	cd $AUTODEPLOY_TOMCAT_HOME/bin
	$AUTODEPLOY_TOMCAT_HOME/bin/shutdown.sh >> $AUTODEPLOY_LOG_FILE
	log_s "Tomcat service is stoped." 
	package;
	rm -rf $AUTODEPLOY_TOMCAT_HOME/work/Catalina/localhost/${APP_NAME};
	mv  $AUTODEPLOY_TOMCAT_HOME/webapps/${APP_NAME}.war $AUTODEPLOY_TOMCAT_HOME/webapps/${APP_NAME}_war.bac;
	rm -rf $AUTODEPLOY_TOMCAT_HOME/webapps/${APP_NAME};
	cp ${GIT_REPO}/${APP_NAME}/target/${APP_NAME}.war $AUTODEPLOY_TOMCAT_HOME/webapps;
	log_s "${APP_NAME}.war deploye completed" 
	sleep 2s
	cd $AUTODEPLOY_TOMCAT_HOME/bin
	$AUTODEPLOY_TOMCAT_HOME/bin/startup.sh >> $AUTODEPLOY_LOG_FILE
	[ $? -eq 0 ] || fail "Tomcat startup faild: $?"
	log_s "Tomcat service is starting..." 
	sleep 5
	tomcatStatusCheck
	[ $? -eq 0 ] || fail "Tomcat service check faild: $?  "
	log_s "Deploy successfully!"	
}

#check tomcat status
function tomcatStatusCheck()
{
	HTTP_CODE=`curl -o /dev/null -s -m 30 --connect-timeout 30 -w %{http_code} $AUTODEPLOY_APP_TEST_URL`
	HTTP_CODE_PRE=${HTTP_CODE:0:1}

	if [[ $HTTP_CODE_PRE -eq 2||$HTTP_CODE_PRE -eq 3 ]]
	then
		log_s "Tomcat service is working fine!"
	else
		fail "Tomcat service startup faild, HTTP_CODE is: $HTTP_CODE"
	fi
}

# main
case $1 in
    compile)
		compile
		;;
    deploy)
        deploy
        ;;
    update)
        updateCode 
        ;;
    package)
        package
        ;;
    status)
        tomcatStatusCheck
        ;;		
    *)
       usage
        ;;
esac
